package cn.mumzone.pos.presenter;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.event.GoodsAmountChangedEvent;
import cn.mumzone.pos.event.MemberChangeEvent;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.source.GoodsRepository;
import cn.mumzone.pos.presenter.contract.GoodsSoldContract;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by JoeLjt on 2019/6/27.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class GoodsSoldPresenter extends AbsPresenter<GoodsSoldContract.View, GoodsRepository> {

    private static final int CODE_SEARCH_EMPTY = 504;

    private int mPageNum = 1;

    private MemberEntity selectMember;

    public void searchGoodsInfo(String goodsCode, final boolean isRefresh, boolean isScan) {
        ApiCallBack<PageListResponse<GoodsEntity>> callBack = new ApiCallBack<PageListResponse<GoodsEntity>>() {
            @Override
            public void onSuccess(PageListResponse<GoodsEntity> response) {
                mView.showSearchGoodsList(response.getList(), isRefresh);
                mPageNum++;
            }

            @Override
            public void onError(BaseResponse response) {
                if (response.getCode() == CODE_SEARCH_EMPTY) {
                    mView.showSearchGoodsError(response.getMsg());
                } else {
                    mView.showError(response.getMsg());
                }
            }

            @Override
            public void onNetworkError(BaseResponse response) {
                mView.showNetError();
            }

        };

        mPageNum = isRefresh ? 1 : mPageNum;
        mRepo.searchGoodsList("", selectMember, goodsCode, StringUtils.get(mPageNum), StringUtils.get(AppConstants.PAGE_SIZE),
                isScan ? "1" : "0", callBack);
        addSubscrebe(callBack);
    }

    public void initEvent() {

        Subscription removeGoodsListener = RxBus.getDefault()
                .toObservable(GoodsAmountChangedEvent.class)
                .subscribe(new Action1<GoodsAmountChangedEvent>() {
                    @Override
                    public void call(GoodsAmountChangedEvent amountChangedEvent) {
                        mView.onGoodsAmountChanged(amountChangedEvent.getGoodsList());
                    }
                });
        addSubscrebe(removeGoodsListener);

        Subscription bindMemberListener = RxBus.getDefault()
                .toObservable(MemberChangeEvent.class)
                .subscribe(new Action1<MemberChangeEvent>() {
                    @Override
                    public void call(MemberChangeEvent memberChangeEvent) {
                        selectMember = memberChangeEvent.getMemberEntity();
                        mView.showMemberBindStatusChanged(selectMember);
                    }
                });
        addSubscrebe(bindMemberListener);

    }

}
